package com.itboye.ihomebank.activity.keytwo;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.ServerError;
import com.android.volley.TimeoutError;
import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.base.MyApplcation;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.bean.SIDBean;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.presenter.KeyPresenter;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;
import com.itboye.ihomebank.util.TimesUtils;
import com.sitri.sdk.SLock;
import com.sitri.sdk.callback.visible.CommonCallback;
import com.sitri.sdk.model.ID;
import com.sitri.sdk.model.ResultData;
import com.sitri.sdk.model.ResultError;

import java.util.Observable;
import java.util.Observer;

/**
 * 白马上传锁数据   绑定管理员
 */
public class UpdateKeyTwoActivity extends BaseOtherActivity implements Observer ,CommonCallback {
    View v_statusbar;
    ImageView close_icon;// 返回图片
    TextView add_shap_title_tv;// 标题

    TextView update_ok;
    KeyPresenter keyPresenter;
    String uid;
    String baima_mac;//绑定的白马mac地址(如果存在代表虽然绑定了锁，但是没有上传到我们的服务器)
    String baima_date;
    @Override
    protected int layout_id() {
        return R.layout.activity_update_key_two;
    }

    @Override
    protected void onInitController() {

    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        SLock.getServices().disconnectBle();
    }

    @Override
    protected void onInit() {
        SLock.getServices().setCommonCallback(this);
        setStatusBarHeight(v_statusbar);
        close_icon.setVisibility(View.GONE);
        add_shap_title_tv.setText("白马锁上传数据");
        keyPresenter = new KeyPresenter(this);
        uid = (String) SPUtils.get(MyApplcation.ctx, null, SPContants.USER_ID, "");
        baima_mac = (String) SPUtils.get(MyApplcation.ctx, null, SPContants.BAIMA_LOCKMAC, "");
        baima_date = (String) SPUtils.get(MyApplcation.ctx, null, SPContants.BAIMA_DATE, "");
    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.close_icon:
                ByAlert.alert(baima_date);
                break;
            case R.id.update_ok:
                showProgressDialog("正在绑定到服务器，请稍后......",false);
                String sid=SPUtils.get(MyApplcation.ctx,null,SPContants.LOCK_SESSION,"")+"";
                if ("".equals(sid)){
                    keyPresenter.initSitri(uid,baima_mac,"Blelock","Blelock", TimesUtils.getStamp(),baima_date);

                }else {
                    keyPresenter.initSitri(sid,baima_mac,"Blelock","Blelock", TimesUtils.getStamp(),baima_date);

                }
                break;
        }
    }

    @Override
    public void update(Observable o, Object arg) {
        ResultEntity entity = handlerError(arg);
        try {
            closeProgressDialog();
        } catch (Exception e) {
        }
        if (entity != null) {
                if (entity.getEventType() == KeyPresenter.initSitri_success) {
                    ByAlert.alert("绑定成功");
                    SPUtils.put(MyApplcation.ctx, null,SPContants.BAIMA_LOCKMAC,"");
                    SPUtils.put(MyApplcation.ctx, null,SPContants.BAIMA_DATE,"");
                    Intent intent2 = new Intent("changeavart");
                    sendBroadcast(intent2);
                    finish();
                } else if (entity.getEventType() == KeyPresenter.initSitri_fail) {
                    if (entity.getData() instanceof ServerError ||entity.getData() instanceof TimeoutError){//服务器失败
                        //切换 锁备用服务器
                        showAlert();
                    }else {
                        ByAlert.alert(entity.getData());
                    }
                }else if (entity.getEventType()== UserPresenter.By_GETSID_success){//获取sid 成功
                    SIDBean data1 = (SIDBean) entity.getData();
                    SPUtils.put(MyApplcation.ctx,null, SPContants.LOCK_SESSION,data1.getSid());
                    SPUtils.put(MyApplcation.ctx,null,SPContants.ISLOCK,true);
                    toRefreshData(data1.getSid());
                }else if (entity.getEventType()==UserPresenter.By_GETSID_fail){
                    ByAlert.alert(entity.getMsg());
                }
        }
    }
    private void toRefreshData(String sid) {
        if ("".equals(sid)){
            keyPresenter.initSitri(uid,baima_mac,"Blelock","Blelock", TimesUtils.getStamp(),baima_date);

        }else {
            keyPresenter.initSitri(sid,baima_mac,"Blelock","Blelock", TimesUtils.getStamp(),baima_date);

        }

    }

    @Override
    public void onSuccess(ResultData resultData) {

    }

    @Override
    public void onFailed(ResultError resultError) {
        log(resultError.getId(), false, resultError.getErrorCode().toLog());
    }
    private void log(ID id, boolean isSuccess, final String log) {
        if (isSuccess) {
            //  ByAlert.alert(id.name()+":第二个");
        } else {
            ByAlert.alert(TostKey.getStamp(log));
        }
    }
}
